package com.qzsoft.zhjd.server.config;

import com.glodon.cloudt.rest.client.exception.AuthenticateException;
import com.glodon.gys.rest.client.impl.GysRestClientNew;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.InputStream;

/**
 * 广联达 物料系统
 */
@Component
public class GysWlRestClientConfig {

    /**
     * http 请求工具
     */
    @Lazy
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 文双2
     */
    @Bean("gysRestClient_ws")
    @Scope("prototype")
    public GysRestClientNew gysRestClient_ws() {
        // TODO：按照厂商说法，授权码是与设备绑定关系。但是可以想办法绕过去！
        ClassPathResource resource = new ClassPathResource("auth/gld_wl_ws.lic");
        GysRestClientNew instance = null;
        try (InputStream inputStream = resource.getInputStream()) {
            instance = GysRestClientNew.getInstance(inputStream);
        } catch (IOException | AuthenticateException e) {
            throw new RuntimeException(e);
        }
        return instance;
    }

    /**
     * 齐临2
     */
    @Bean("gysRestClient_ql")
    @Scope("prototype")
    public GysRestClientNew gysRestClient_ql() {
        // TODO：按照厂商说法，授权码是与设备绑定关系。但是可以想办法绕过去！
        ClassPathResource resource = new ClassPathResource("auth/gld_wl_ql.lic");
        GysRestClientNew instance = null;
        try (InputStream inputStream = resource.getInputStream()) {
            instance = GysRestClientNew.getInstance(inputStream);
        } catch (IOException | AuthenticateException e) {
            throw new RuntimeException(e);
        }
        return instance;
    }

    /**
     * 济德1
     */
    @Bean("gysRestClient_jd")
    @Scope("prototype")
    public GysRestClientNew gysRestClient_jd() {
        // TODO：按照厂商说法，授权码是与设备绑定关系。但是可以想办法绕过去！
        ClassPathResource resource = new ClassPathResource("auth/gld_wl_jd1.lic");
        GysRestClientNew instance = null;
        try (InputStream inputStream = resource.getInputStream()) {
            instance = GysRestClientNew.getInstance(inputStream);
        } catch (IOException | AuthenticateException e) {
            throw new RuntimeException(e);
        }
        return instance;
    }
}
